package com.xywh.wanandroid.ext

import android.os.Bundle
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import androidx.navigation.NavController
import androidx.navigation.NavDeepLinkRequest
import androidx.navigation.NavDestination
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.NavOptions
import androidx.navigation.Navigator
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations

@Composable
fun LazyListState.isScrollDown(): Boolean {


    //当state处于滚动时，preScrollStartOffset会被初始化并记忆,不会再被更改
    //记录上一次第一个可见item的滑动偏移
    val lastFirstVisibleItemScrollOffset by remember {
        mutableIntStateOf(
            firstVisibleItemScrollOffset
        )
    }
    //记录上一次第一个可见item下标
    val lastFirstVisibleItemIndex by remember { mutableIntStateOf(firstVisibleItemIndex) }

    return remember(this) {
        derivedStateOf {
            if (firstVisibleItemIndex > lastFirstVisibleItemIndex) {
                //第一个可见item的index大于开始滚动时第一个可见item的index，说明 往下滚动了

                true
            } else if (firstVisibleItemIndex < lastFirstVisibleItemIndex) {
                //第一个可见item的index小于开始滚动时第一个可见item的index，说明 往上滚动了

                false
            } else {
                //第一个可见item的index等于开始滚动时第一个可见item的index,对比item offset
                firstVisibleItemScrollOffset > lastFirstVisibleItemScrollOffset
            }
        }
    }.value

}

@Composable
fun LoadingDialog(openLoadingDialog: String) {
    if (openLoadingDialog.isNotEmpty())

        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0x80666666)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .padding(20.dp)
                    .fillMaxWidth()
                    .height(100.dp)
                    .clip(RoundedCornerShape(10))
                    .background(Color.White)

            ) {

                CircularProgressIndicator(
                    Modifier
                        .padding(start = 20.dp)
                        .size(30.dp),
                    strokeWidth = 3.dp,
                    color = MaterialTheme.colorScheme.primary
                )
                Spacer(Modifier.requiredWidth(30.dp))
                Text(
                    text = openLoadingDialog,
                    fontSize = 14.sp,
                    color = Color.Black
                )
            }


        }
}



fun navigateUpWan(navController: NavHostController) {
    navController.navigateUp()
}

fun toComposeWebView(navController: NavHostController, title: String, link: String) {
    navController.navigate("${WanAndroidDestinations.WEB_VIEW_ROUTE}?title=${title}&&link=${link}")
}

fun NavController.navigateBundle(
    route: String,
    args: Bundle,
    navOptions: NavOptions? = null,
    navigatorExtras: Navigator.Extras? = null
) {
    val routeLink = NavDeepLinkRequest
        .Builder
        .fromUri(NavDestination.createRoute(route).toUri())
        .build()

    val deepLinkMatch = graph.matchDeepLink(routeLink)
    if (deepLinkMatch != null) {
        val destination = deepLinkMatch.destination
        val id = destination.id
        navigate(id, args, navOptions, navigatorExtras)
    } else {
        navigate(route, navOptions, navigatorExtras)
    }
}


fun navigateRoute(navController: NavHostController, route: String) {
    navController.navigate(route) {
        // Pop up to the start destination of the graph to
        // avoid building up a large stack of destinations
        // on the back stack as users select items
        popUpTo(navController.graph.findStartDestination().id) {
            saveState = true
        }
        // Avoid multiple copies of the same destination when
        // reselecting the same item
        launchSingleTop = true
        // Restore state when reselecting a previously selected item
        restoreState = true
    }
}
